<template>
  <div class="applyOf login">
    <div class="flex justify_between align_center">
      <div
        class="underline"
        style="width:120px;"
      >
        <span class="title1 color1 zIndex fontBold">微信支付</span>
      </div>
      <div
        class="customer flex justify_center align_center"
        @click="show = true"
      >
        <svg
          class="icon"
          aria-hidden="true"
        >
          <use xlink:href="#icon-lianxiwomen" />
        </svg>
        <span class="font400 content3">联系我们</span>
      </div>
    </div>
    <div class="login_content">
      <div class="contentInfo">
        <div class="contentInfoBox flex justify_between flex_column">
          <p class="font400 content3 color3">
            <span
              class="font400 content3 color3"
              v-if="data.pay_type == 1 && data.is_apply == 0"
            >会员续费</span>
            <span
              class="font400 content3 color3"
              v-if="data.pay_type == 2"
            >活动报名
            </span>
            <span
              class="font400 content3 color3"
              v-if="data.pay_type == 3"
            >课程报名
            </span>
            <span
              class="font400 content3 color3"
              v-if="data.pay_type == 1 && data.is_apply == 1"
            >企业入会
            </span>
            / {{ data.title }}
          </p>
          <p class="font400 content2 color6">
            {{ data.create_time }}
          </p>
        </div>
      </div>
      <div class="Membership">
        <div class="MembershipBox">
          <div class="MembershipBoxHeader flex justify_between align_center">
            <p class="content4 color2 font400">
              ￥{{ data.price }}
            </p>
          </div>
        </div>
      </div>
      <div class="ewm flex justify_between align_center">
        <div class="ewm_box flex justify_between align_center">
          <div class="ewm_left">
            <img src="/src/assets/img/logo.png">
            <p class="font400 content3 color3">
              长按关注深圳房协公众号
              了解我们的最新动态
            </p>
          </div>
          <div class="ewm_right">
            <img src="/src/assets/img/ewm.png">
          </div>
        </div>
      </div>
      <van-dialog
        v-model:show="show"
        title="温馨提示"
        confirm-button-color="#FF7A1F"
      >
        <div class="Scan">
          <p class="font400 content3 color3">
            您可长按添加工作人员微信 与我们取得联系
          </p>
          <img src="@/assets/img/no-content-color.png">
        </div>
      </van-dialog>
      <van-dialog v-model:show="showQrCode" @confirm="closeQrCode" title="温馨提示" :close-on-click-overlay="false" confirm-button-color="#FF7A1F">
        <div class="Scan">
          <p class="font400 content3 color3">
            长按识别二维码，进入本期活动微信群
          </p>
          <img :src="qrCodeUrl">
        </div>
      </van-dialog>
      <p class="understand content3 font400 color3">
        了解深圳房地产协会 >
      </p>
    </div>
  </div>
  <div class="fexd_footer">
    <div v-if="data.status == 1" class="footer_btn flex align_center justify_between">
      <div
        class="font400 content3 color4"
        @click="back"
      >
        <svg
          class="icon"
          aria-hidden="true"
        >
          <use xlink:href="#icon-fanhui" />
        </svg>
        <p class="content2 font400 color3">
          返回
        </p>
      </div>
      <div
        @click="reapply"
        class="btn_item qr font400 content3 color4"
      >
        立即支付
      </div>
    </div>
    <div
      v-if="data.status == 2 && data.is_invoice == 1"
      class="footer_btn flex align_center justify_between"
    >
      <div
        class="font400 content3 color4"
        @click="back"
      >
        <svg
          class="icon"
          aria-hidden="true"
        >
          <use xlink:href="#icon-fanhui" />
        </svg>
        <p class="content2 font400 color3">
          返回
        </p>
      </div>
      <div
        @click="handleGetAuthUrl"
        class="btn_item qr font400 content3 color4"
      >
        申请开票
      </div>
    </div>

    <div
      @click="back"
      class="btn_item cancle font400 content3 color4"
      v-else
    >
      返回
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { orderInfo, orderReply, getActivityQrcode } from '@/api'
import { useRouter, useRoute } from 'vue-router'
import { Dialog, Toast } from 'vant'
import { useStore } from 'vuex'
const show: any = ref(false)
const data: any = ref([])
const router = useRouter()
const route = useRoute()
const store = useStore()
const showQrCode: any = ref(false)
const qrCodeUrl: any = ref('')

const getOrder = async () => {
  data.value = await orderInfo({ order_id: route.query.orderId })
  await getQrcode()
  console.log(data)
}

getOrder()

const back = () => {
  if (store.state.from.path !== '/') {
    router.back()
  } else {
    router.replace('/userCenter')
  }
}

const getQrcode = () => {
  console.log('data.value.paid_id', data.value)
  if (route.query.active_id || data.value.paid_id) {
    getActivityQrcode({ activity_id: route.query.active_id || data.value.paid_id }).then((res: any) => {
      if (res.url) {
        showQrCode.value = true
        qrCodeUrl.value = res.url
      }
    })
  }
}

const handleGetAuthUrl = () => {
  // otherServices/Invoicing
  // invoiceGetAuthUrl({ order_id: data.value.id }).then((res: any) => {
  //   window.location.href = res.auth_url
  // })
  router.replace('/otherServices/cost')
}

const closeQrCode = () => {
  showQrCode.value = false
}

const reapply = async () => {
  try {
    const res: any = await orderReply({ order_id: route.query.orderId })
    const ua: any = navigator.userAgent.toLowerCase()
    if (ua.match(/MicroMessenger/i) != null && ua.match(/MicroMessenger/i)[0] === 'micromessenger') {
      // eslint-disable-next-line no-undef
      WeixinJSBridge.invoke(
        'getBrandWCPayRequest', {
          appId: res.appId,
          timeStamp: res.timeStamp,
          nonceStr: res.nonceStr,
          package: res.package,
          signType: res.signType,
          paySign: res.sign
        },
        function (rulut: any) {
          if (rulut.err_msg === 'get_brand_wcpay_request:ok') {
            Dialog.alert({
              title: '温馨提示',
              message: '支付成功'
            }).then(() => {
              router.replace({ path: '/find/activity', query: { id: route.query.paid_id, success: 1 } })
            })
          } else if (rulut.err_msg === 'get_brand_wcpay_request:cancel') {
            router.replace('/otherServices/order')
          } else {
            Dialog.alert({
              title: '温馨提示',
              message: '支付失败'
            }).then(() => {
              router.replace('/otherServices/order')
            })
          }
        })
    } else {
      Dialog.alert({
        title: '温馨提示',
        message: '请用手机微信进入平台支付'
      })
    }
  } catch (error: any) {
    Toast(error.message)
  }

  //   router.replace('/agreement')
}
</script>

<style lang="less" scoped>
.cancle {
      width: 90%;
      height: 44px;
      background: #85898c;
      border-radius: 5px;
      text-align: center;
      line-height: 44px;
      margin: 4vw auto;
    }
.applyOf {
  padding: 0 30px 80px;
  margin-top: 40px;
  .customer {
    width: 93px;
    height: 32px;
    border: 1px solid #ff7a1f;
    border-radius: 5px;
    width: 111px;
    line-height: 32px;
    text-align: center;
    .icon {
      width: 22px;
      height: 22px;
    }
    span {
      margin-left: 4px;
      color: #ff7a1f;
    }
  }
  .login_content {
    margin-top: 20px;

    .contentInfo {
      width: 100%;
      background: #fff;
      border-radius: 10px;
      margin: 20px 0;
      .contentInfoBox {
        padding: 15px;
        p:last-child {
          margin-top: 25px;
        }
      }
    }
    .understand {
      margin-top: 30px;
      text-align: center;
      text-decoration: underline;
    }
    .applyOf_tisp {
      width: 100%;
      height: 286px;
      background: #ffffff;
      border-radius: 5px;
      text-align: center;
      img {
        width: 170px;
        height: 150px;
        object-fit: contain;
        margin: 0 auto 20px;
      }
      p:last-child {
        margin-top: 10px;
      }
    }
    .ewm {
      width: 100%;
      height: 114px;
      background: #ffffff;
      border-radius: 5px;
      margin-top: 16px;
      .ewm_box {
        width: 100%;
        padding: 0 15px;
        .ewm_left {
          img {
            width: 100px;
            height: 30px;
            object-fit: contain;
          }
          p {
            width: 180px;
            margin-top: 14px;
            line-height: 18px;
          }
        }
        .ewm_right {
          width: 74px;
          height: 74px;
          img {
            width: 74px;
            height: 74px;
            object-fit: contain;
          }
        }
      }
    }
  }
  .leftw {
    width: 90px;
    display: inline-block;
  }
  .line {
    margin: 0 30px;
  }
  .setBnt {
    width: 98px;
    height: 30px;
    background: #12142e;
    border-radius: 5px;
    font-size: 14px;
    font-weight: 400;
  }
  .Scan {
    p {
      width: 190px;
      margin: 18px auto;
      text-align: center;
      line-height: 20px;
    }
    img {
      width: 120px;
      height: 120px;
      object-fit: contain;
      margin: 0 auto 20px auto;
    }
  }
  .Membership {
    width: 100%;
    background: #ffffff;
    border-radius: 5px;
    .MembershipBox {
      padding: 15px;
    }
    .MembershipContent {
      margin-top: 30px;
      margin-bottom: 30px;
      p {
        line-height: 20px;
      }
      p:last-child {
        margin-top: 20px;
      }
    }
    .MembershipInfo {
      margin-bottom: 20px;
      p {
        margin-bottom: 5px;
        span:last-child {
          margin-left: 5px;
        }
      }
    }
    .copy {
      width: 82px;
      height: 30px;
      background: #12142e;
      border-radius: 5px;
      text-align: center;
      line-height: 30px;
    }
  }
}
.fexd_footer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 70px;
  background-color: #fff;
  box-shadow: 0px 2px 12px 2px rgba(22, 12, 6, 0.09);
  .icon {
    width: 23px;
    height: 23px;
    margin-bottom: 2px;
  }
  .footer_btn {
    padding: 0 30px;
    height: 100%;
    .cancle {
      width: 100%;
      height: 44px;
      background: #85898c;
      border-radius: 5px;
      text-align: center;
      line-height: 44px;
    }
    .qr {
      width: 189px;
      height: 44px;
      background: #ff7a1f;
      border-radius: 5px;
      text-align: center;
      line-height: 44px;
    }
  }
}
@media (min-width: 750px) {
  .applyOf {
    padding: 0 30px 80px;
    margin-top: 40px;
    .customer {
      width: 93px;
      height: 32px;
      border: 1px solid #ff7a1f;
      border-radius: 5px;
      width: 111px;
      line-height: 32px;
      text-align: center;
      .icon {
        width: 22px;
        height: 22px;
      }
      span {
        margin-left: 4px;
        color: #ff7a1f;
      }
    }
    .login_content {
      margin-top: 20px;

      .contentInfo {
        width: 100%;
        background: #fff;
        border-radius: 10px;
        margin: 20px 0;
        .contentInfoBox {
          padding: 15px;
          p:last-child {
            margin-top: 25px;
          }
        }
      }
      .understand {
        margin-top: 30px;
        text-align: center;
        text-decoration: underline;
      }
      .applyOf_tisp {
        width: 100%;
        height: 286px;
        background: #ffffff;
        border-radius: 5px;
        text-align: center;
        img {
          width: 170px;
          height: 150px;
          object-fit: contain;
          margin: 0 auto 20px;
        }
        p:last-child {
          margin-top: 10px;
        }
      }
      .ewm {
        width: 100%;
        height: 114px;
        background: #ffffff;
        border-radius: 5px;
        margin-top: 16px;
        .ewm_box {
          width: 100%;
          padding: 0 15px;
          .ewm_left {
            img {
              width: 100px;
              height: 30px;
              object-fit: contain;
            }
            p {
              width: 180px;
              margin-top: 14px;
              line-height: 18px;
            }
          }
          .ewm_right {
            width: 74px;
            height: 74px;
            img {
              width: 74px;
              height: 74px;
              object-fit: contain;
            }
          }
        }
      }
    }
    .leftw {
      width: 90px;
      display: inline-block;
    }
    .line {
      margin: 0 30px;
    }
    .setBnt {
      width: 98px;
      height: 30px;
      background: #12142e;
      border-radius: 5px;
      font-size: 14px;
      font-weight: 400;
    }
    .Scan {
      p {
        width: 190px;
        margin: 18px auto;
        text-align: center;
        line-height: 20px;
      }
      img {
        width: 95px;
        height: 95px;
        object-fit: contain;
        margin: 0 auto;
      }
    }
    .Membership {
      width: 100%;
      background: #ffffff;
      border-radius: 5px;
      .MembershipBox {
        padding: 15px;
      }
      .MembershipContent {
        margin-top: 30px;
        margin-bottom: 30px;
        p {
          line-height: 20px;
        }
        p:last-child {
          margin-top: 20px;
        }
      }
      .MembershipInfo {
        margin-bottom: 20px;
        p {
          margin-bottom: 5px;
          span:last-child {
            margin-left: 5px;
          }
        }
      }
      .copy {
        width: 82px;
        height: 30px;
        background: #12142e;
        border-radius: 5px;
        text-align: center;
        line-height: 30px;
      }
    }
  }
  .fexd_footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 70px;
    background-color: #fff;
    box-shadow: 0px 2px 12px 2px rgba(22, 12, 6, 0.09);
    .icon {
      width: 23px;
      height: 23px;
      margin-bottom: 2px;
    }
    .footer_btn {
      padding: 0 30px;
      height: 100%;
      .cancle {
        width: 100%;
        height: 44px;
        background: #85898c;
        border-radius: 5px;
        text-align: center;
        line-height: 44px;
      }
      .qr {
        width: 189px;
        height: 44px;
        background: #ff7a1f;
        border-radius: 5px;
        text-align: center;
        line-height: 44px;
      }
    }
  }
}
</style>
